<?php
namespace Home\Controller;
use Common\Controller\CommonController;
use Home\Model\User;

class UserController extends CommonController {
	
	function _initialize(){
		parent::_initialize();
		$LG_USER = session('LG_USER');
		if(empty($LG_USER)){
        	$this->error('请先登录', U('/Public/login')); 
        } 
		$this->assign('LG_USER',session('LG_USER')); 
	}
	//用户列表
	function index(){
        $balance = M('user');
        $username=$_GET['username'];
        if(empty($_GET['p'])){
            $_GET['p']=1;
        }
        $condition="";
        if(!empty($username)){
            $condition="username like '%".$username."%'";
        }
        $list = $balance->where($condition)->order('id desc')->page($_GET['p'].',25')->select();
        $this->assign('list',$list);// 赋值数据集
        $count      = $balance->where($condition)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Pager($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->showhtml();// 分页显示输出

        $this->assign('img_url',$_SERVER['HTTP_HOST'].__ROOT__."/");// 赋值分页输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("username",$username);
        $this->display(); // 输出模板
	}
	//用户转入余额  选择用户信息
	function select(){
        $balance = M('user_balance');
        $username=$_GET['username'];
        if(empty($_GET['p'])){
            $_GET['p']=1;
        }
        if($_GET['type']==0){
            $condition="balance>0";
        }else{
            $condition="balance>=0";
        }
        if(!empty($username)){
            $condition.=" AND username like '%".$username."%'";
        }
        if(!empty($_GET['ids'])){
             $condition.=" AND user_id not in(".$_GET['ids'].")";
        }

        $list = $balance->where($condition)->order('id desc')->page($_GET['p'].',25')->select();
        $this->assign('list',$list);// 赋值数据集
        $count      = $balance->where($condition)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Pager($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->showhtml();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('ids',$_GET['ids']);// 赋值分页输出
        $this->assign("username",$username);
        $this->assign("type",$_GET['type']);

        $this->display(); // 输出模板
	}

    public function update($id){
        $model=new User();
        $model->where("id=".$id)->find();
        if(IS_POST && $model->data($_POST) &&  $model->update()){
            $this->assign('msg',"产品修改成功");
            $this->display("iframe/success");
        }
        $this->assign('error',$model->getError());
        $this->assign('model',$model->getData());
        $this->assign('img_url',$_SERVER['HTTP_HOST'].__ROOT__."/");// 赋值分页输出
        $this->display(); // 输出模板
    }

}